package iofile;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader_ {

    public static void main(String[] args) {

    }

    //读取单个字符
    public void read_01(){
        String filePath = "";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(filePath);
            //read()方法每次读取单个字符
            while ((data = fileReader.read()) != -1){
                System.out.println((char) data);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    //使用字符数组读取文件
    public void read_02(){
        String filePath = "";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        try {
            fileReader = new FileReader(filePath);
            //根据传入字符数组的长度，读取字符
            while ((readLen = fileReader.read(buf)) != -1){
                System.out.println(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }


}
